package breeze.pixel.weather.weather_forecast.model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.List;

import breeze.pixel.weather.R;
import breeze.pixel.weather.adapter.weather.DailyWeatherAdapter;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;

public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ViewHolder> {
    private final Context context;
    private final List<DailyWeatherAdapter.DailyBean> datas;
    private onItemClickListner onClick;
    public ForecastAdapter(Context context, List<DailyWeatherAdapter.DailyBean> list){
        this.context = context;
        this.datas = list;
    }
    @NonNull
    @Override
    public ForecastAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.forecast_item , null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ForecastAdapter.ViewHolder holder, int position) {
        DailyWeatherAdapter.DailyBean bean = datas.get(position);
        holder.mTem.setText(String.format("%s~%s℃", bean.getTempMin(), bean.getTempMax()));
        holder.mWeather.setText(bean.getTextDay());
        holder.mWet.setText(String.format("湿度：%s", bean.getHumidity()));
        holder.mWind.setText(String.format("%s级",bean.getWindDirDay()+bean.getWindScaleDay()));
        holder.mTime.setText(bean.getFxDate());
        holder.mIcon.setImageBitmap(AppToolUtil.getweatherIcon(context, bean.getIconDay()));
        holder.mBackground.setOnClickListener(v -> {
            if(onClick!=null){
                onClick.onItemClick(bean);
            }
        });
    }

    public interface onItemClickListner{
        void onItemClick(DailyWeatherAdapter.DailyBean bean);
    }

    public void setOnItemClickListenr(onItemClickListner listenr){
        this.onClick = listenr;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        MTextView mTime,mWeather,mTem,mWind,mWet;
        RelativeLayout mBackground;
        ImageView mIcon;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTime = itemView.findViewById(R.id.h_item_time);
            mWeather = itemView.findViewById(R.id.h_item_weather);
            mTem = itemView.findViewById(R.id.h_item_tem);
            mWind = itemView.findViewById(R.id.h_item_wind);
            mWet = itemView.findViewById(R.id.h_item_wet);
            mIcon = itemView.findViewById(R.id.h_item_w_icon);
            mBackground = itemView.findViewById(R.id.h_item_background);
        }
    }
}
